package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;

import com.heima.model.wemeida.dtos.WmSensitivPageReqeDto;
import com.heima.model.wemeida.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive>  implements WmSensitiveService {
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    /**
     * 平台分页查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<List<WmSensitive>> findList(WmSensitivPageReqeDto dto ) {
        //校验参数
        dto.checkParam();
        //获取当前用户
/*       AdUser adUser = (AdUser) ThreadLocalUtils.get();
       if (adUser==null){
           throw  new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
       }   */


       /*
       分页
        */

       IPage<WmSensitive> iPage =new Page<>(dto.getPage(),dto.getSize());

        QueryWrapper<WmSensitive> wrapper =new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getName())){
            wrapper.like("sensitives",dto.getName());
        }
        wrapper.orderByDesc("created_time");
        iPage=page(iPage,wrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        pageResponseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        pageResponseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());

        return pageResponseResult;
    }

    /**
     * 新增敏感词
     */
    @Override
    public void saveMGC(WmSensitive wmSensitive) {
       if (StringUtils.isEmpty(wmSensitive.getSensitives())){
           throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
       }
       //判断敏感词是否已经存在
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        WmSensitive wmSensitive1 = wmSensitiveMapper.selectOne(wrapper);
        if (wmSensitive1!=null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_EXIST);
        }

        WmSensitive wmSensitive2 = new WmSensitive();

        wmSensitive2.setSensitives(wmSensitive.getSensitives());
        wmSensitive2.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive2);
    }

    @Override
    public ResponseResult deleteMGC(Integer id) {
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     */
    @Override
    public void updateMGC(WmSensitive wmSensitive) {

        if (StringUtils.isEmpty(wmSensitive.getSensitives())){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive1 = new WmSensitive();
        wmSensitive1.setId(wmSensitive.getId());
        wmSensitive1.setSensitives(wmSensitive.getSensitives());
        wmSensitive1.setCreatedTime(new Date());


        wmSensitiveMapper.updateById(wmSensitive1);

    }
}
